package com.ryder.subject.application.controller;

import com.alibaba.fastjson2.JSON;
import com.google.common.base.Preconditions;
import com.ryder.subject.application.convert.SubjectCollectDTOConverter;
import com.ryder.subject.application.dto.SubjectCollectDTO;
import com.ryder.subject.application.dto.SubjectLikedDTO;
import com.ryder.subject.common.entity.PageResult;
import com.ryder.subject.common.entity.Result;
import com.ryder.subject.common.util.LoginUtil;
import com.ryder.subject.domain.entity.SubjectCollectBO;
import com.ryder.subject.domain.service.SubjectCollectDomainService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 描述: 题目收藏表 controller
 *
 * @author ryder
 * @version 1.0
 * @since 1.0 // 版本号，指明从哪个版本开始
 * 更新日期: 2025-03-16 11:29:20
 */
@RestController
@RequestMapping("/subjectCollect/")
@Slf4j
public class SubjectCollectController {

    @Resource
    private SubjectCollectDomainService subjectCollectDomainService;

    /**
     * 新增题目点赞表
     */
    @RequestMapping("add")
    public Result<Boolean> add(@RequestBody SubjectCollectDTO subjectCollectDTO) {

        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectCollectController.add.dto:{}", JSON.toJSONString(subjectCollectDTO));
            }
            Preconditions.checkNotNull(subjectCollectDTO.getSubjectId(), "题目id不能为空");
            Preconditions.checkNotNull(subjectCollectDTO.getStatus(), "收藏状态 1收藏 0不收藏不能为空");
            String loginId = LoginUtil.getLoginId();
            subjectCollectDTO.setLikeUserId(loginId);
            Preconditions.checkNotNull(subjectCollectDTO.getLikeUserId(), "收藏人id不能为空");
            SubjectCollectBO SubjectCollectBO = SubjectCollectDTOConverter.INSTANCE.convertDTOToBO(subjectCollectDTO);
            subjectCollectDomainService.add(SubjectCollectBO);
            return Result.ok(true);
        } catch (Exception e) {
            log.error("SubjectCollectController.register.error:{}", e.getMessage(), e);
            return Result.fail("新增题目点赞表失败");
        }

    }

    /**
     * 修改题目点赞表
     */
    @RequestMapping("update")
    public Result<Boolean> update(@RequestBody SubjectCollectDTO subjectCollectDTO) {

        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectCollectController.update.dto:{}", JSON.toJSONString(subjectCollectDTO));
            }
            Preconditions.checkNotNull(subjectCollectDTO.getId(), "主键不能为空");
            Preconditions.checkNotNull(subjectCollectDTO.getSubjectId(), "题目id不能为空");
            Preconditions.checkNotNull(subjectCollectDTO.getLikeUserId(), "收藏人id不能为空");
            Preconditions.checkNotNull(subjectCollectDTO.getStatus(), "收藏状态 1收藏 0不收藏不能为空");
            Preconditions.checkNotNull(subjectCollectDTO.getCreatedBy(), "创建人不能为空");
            Preconditions.checkNotNull(subjectCollectDTO.getCreatedTime(), "创建时间不能为空");
            Preconditions.checkNotNull(subjectCollectDTO.getUpdateBy(), "修改人不能为空");
            Preconditions.checkNotNull(subjectCollectDTO.getUpdateTime(), "修改时间不能为空");
            Preconditions.checkNotNull(subjectCollectDTO.getIsDeleted(), "不能为空");
            SubjectCollectBO subjectCollectBO = SubjectCollectDTOConverter.INSTANCE.convertDTOToBO(subjectCollectDTO);
            return Result.ok(subjectCollectDomainService.update(subjectCollectBO));
        } catch (Exception e) {
            log.error("SubjectCollectController.update.error:{}", e.getMessage(), e);
            return Result.fail("更新题目点赞表信息失败");
        }

    }

    /**
     * 删除题目点赞表
     */
    @RequestMapping("delete")
    public Result<Boolean> delete(@RequestBody SubjectCollectDTO subjectCollectDTO) {

        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectCollectController.delete.dto:{}", JSON.toJSONString(subjectCollectDTO));
            }
            Preconditions.checkNotNull(subjectCollectDTO.getId(), "主键不能为空");
            Preconditions.checkNotNull(subjectCollectDTO.getSubjectId(), "题目id不能为空");
            Preconditions.checkNotNull(subjectCollectDTO.getLikeUserId(), "收藏人id不能为空");
            Preconditions.checkNotNull(subjectCollectDTO.getStatus(), "收藏状态 1收藏 0不收藏不能为空");
            Preconditions.checkNotNull(subjectCollectDTO.getCreatedBy(), "创建人不能为空");
            Preconditions.checkNotNull(subjectCollectDTO.getCreatedTime(), "创建时间不能为空");
            Preconditions.checkNotNull(subjectCollectDTO.getUpdateBy(), "修改人不能为空");
            Preconditions.checkNotNull(subjectCollectDTO.getUpdateTime(), "修改时间不能为空");
            Preconditions.checkNotNull(subjectCollectDTO.getIsDeleted(), "不能为空");
            SubjectCollectBO subjectCollectBO = SubjectCollectDTOConverter.INSTANCE.convertDTOToBO(subjectCollectDTO);
            return Result.ok(subjectCollectDomainService.delete(subjectCollectBO));
        } catch (Exception e) {
            log.error("SubjectCollectController.delete.error:{}", e.getMessage(), e);
            return Result.fail("删除题目点赞表信息失败");
        }

    }

    /**
     * 查询我的点赞列表
     *
     * @return Result true：成功 false：失败
     */
    @PostMapping("/getSubjectCollectPage")
    public Result<PageResult<SubjectLikedDTO>> getSubjectCollectPage(@RequestBody SubjectCollectDTO subjectCollectDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectCollectController.getSubjectCollectPage.dto:{}", JSON.toJSONString(subjectCollectDTO));
            }
            //转bo
            SubjectCollectBO subjectCollectBO = SubjectCollectDTOConverter.INSTANCE.convertDTOToBO(subjectCollectDTO);
            log.info("SubjectCollectController.getSubjectLikedPage.bo:{}", JSON.toJSONString(subjectCollectBO));
            subjectCollectBO.setPageNo(subjectCollectDTO.getPageNo());
            subjectCollectBO.setPageSize(subjectCollectDTO.getPageSize());
            PageResult<SubjectCollectBO> boPageResult = subjectCollectDomainService.getSubjectCollectPage(subjectCollectBO);
            log.info("SubjectCollectController.getSubjectLikedPage.boPageResult:{}", JSON.toJSONString(boPageResult));

            return Result.ok(boPageResult);
        } catch (Exception e) {
            log.error("SubjectCollectController.getSubjectCollectPage.error:{}", e.getMessage(), e);
            return Result.fail("分页查询我的点赞失败");
        }
    }

}
